<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <template  v-if="this.$route.query.id!=undefined">
          <!-- 面包屑 -->
          <el-breadcrumb separator-class="el-icon-arrow-right" >
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>
              <template>
                {{this.$route.query.name}}
              </template>
            </el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
          <!-- 分割线 -->
          <el-divider ></el-divider>
        </template>
<!--    :model="formInline"    v-model="formInline.region"     v-model="formInline.user"  -->
<!-- 选择框 -->
<div class="class">
  <el-form :inline="true"  class="demo-form-inline" :model="Form">
  <el-form-item label="目录名称">
    <el-input v-model="Form.directoryName"  ></el-input>
  </el-form-item>
  <el-form-item label="状态">
    <el-select v-model="Form.state" placeholder="请选择">
      <el-option label="禁用" value="0"></el-option>
      <el-option label="启用" value="1"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>

      <el-button @click="clear">清除</el-button>
    <el-button type="primary" @click="onSubmit" >搜索</el-button>

  </el-form-item>
     <div class="right">
      <el-page-header v-if="this.$route.query.id!=undefined" title="返回学科"  class="back"
      @click.native="toList()"
      />
      <el-button type="success" @click="addir" class="dir">新增目录</el-button>
    </div>
  </el-form>
</div>
<!-- 数据 -->
<el-alert
:title='alertText'
type="info"
show-icon
:closable="false">
</el-alert>
<!-- 表格 -->
<el-table
    :data="items"
    style="width: 100%">
    <el-table-column
      type="index"
      label="序号"
      :index="indexMethod"
      width="150">
    </el-table-column>
    <el-table-column
     prop="subjectName"
      label="所属学科"
      width="180">
    </el-table-column>
    <el-table-column
    prop="directoryName"
      label="目录名称"
      width="200">
    </el-table-column>
    <el-table-column
    prop="username"
      label="创建者"
      width="180">
    </el-table-column>
    <el-table-column

      prop="addDate"
        label="创建日期"
        width="280">
        <template #default="{row}">
      {{ formateTime(row.addDate) }}
    </template>
      </el-table-column>

    <el-table-column
    prop="totals"
      label="面试题数量"
      width="150">
    </el-table-column>
    <el-table-column
    prop="state"
    :formatter="formatterFormOfEmpment"
      label="状态"
      width="150">
    </el-table-column>
    <el-table-column

      label="操作"
      width="120">
      <template slot-scope="{row}">
        <el-button type="text"  size="small" @click=" toggleState(row)" > {{ row.state===1? '禁用': '启用' }} </el-button>

        <el-button  type="text"  size="small" @click="editList(row)" :disabled="row.state===1"> 修改 </el-button>

        <el-button type="text" size="small" @click="delList(row.id)" :disabled="row.state===1"> 删除  </el-button>
       </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="pager" style="margin:10px;display:flex;justify-content:flex-end">

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="data.page"
        :page-size="data.pagesize"
        layout="prev, pager, next, jumper,sizes"
        :total="counts">
      </el-pagination>
  </div>
      </el-card>
    </div>
    <disrectorys-add :showDialog.sync="showDialog" :nodeData="nodeData" :form="form" />
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { list as getList, remove as removeList, changeState as changeStateAPI } from '@/api/hmmm/directorys'
import dayjs from 'dayjs'
import disrectorysAdd from '../components/directorys-add.vue' // 添加弹窗

export default {
  components: {
    disrectorysAdd
  },
  created () {
    getCompanys()
    getQuestion()
    // 渲染列表
    this.dataList()
  },
  data () {
    return {
      page1: '目录管理',
      data: {
        page: 1,
        pagesize: 10,
        // directoryName: ''
        // state: ,
        subjectID: this.$route.query.id
      },
      items: [],
      counts: 0,
      disabled: false,
      showDialog: false,
      Form: {
        directoryName: '',
        state: ''
      },
      alertText: '共0条',
      nodeData: {},
      form: {
        subjectID: '',
        directoryName: ''
      }

    }
  },
  methods: {
    /**
     * 请求数据
     * 渲染列表
     */
    async dataList () {
      const { data } = await getList(this.data)
      this.items = data.items
      this.counts = data.counts
      this.alertText = '数据一共' + this.counts + '条'
    },
    /**
     * 计算索引
     */
    indexMethod (index) {
      return (this.data.page - 1) * this.data.pagesize + index + 1
    },

    /**
     * 页容量变化
     */
    handleSizeChange (val) {
      this.data.pagesize = val
      this.data.page = 1
      this.dataList()
    },
    // /**
    //  * 页码变化
    //  */
    handleCurrentChange (val) {
      this.data.page = val
      this.dataList()
    },
    /**
     * 时间格式转换
     */
    formateTime (time) {
      return dayjs(time).format('YYYY-MM-DD')
    },
    /**
     * 格式化启用禁用状态
     * @param {*} row
     * @param {*} column
     * @param {*} cellValue
     * @param {*} index
     */
    formatterFormOfEmpment (row, column, cellValue, index) {
      if (Number(cellValue) === 1) {
        return '已启用'
      } else if (Number(cellValue) === 0) {
        return '已禁用'
      }
    },
    /**
     * 删除数据
     * @param {*} id
     */
    delList (id) {
      const deleteData = {
        id: id
      }
      this.$confirm('您确认删除这条数据嘛？', '温馨提示')
        .then(async () => {
          await removeList(deleteData)
          if (this.items.length === 1 && this.data.page > 1) {
            this.data.page--
          }
          this.dataList()
          this.$message.success('删除成功')
        }).catch(() => {})
    },
    /**
     * 点击新增弹窗出现
     */
    addir () {
      this.showDialog = true
      if (this.$route.query.id !== undefined) {
        this.nodeData = { subjectID: parseInt(this.$route.query.id) }
      } else {
        this.nodeData = {}
      }
    },
    /**
     * 点击修改
     */
    editList (data) {
      this.showDialog = true
      if (this.$route.query.id !== undefined) {
        this.nodeData = {
          id: data.id,
          subjectID: parseInt(this.$route.query.id)
        }
      } else {
        this.nodeData = {
          id: data.id
        }
      }

      this.form = data
    },
    // 启用 禁用
    async toggleState (row) {
      await changeStateAPI({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
    },
    // 点击搜索
    onSubmit () {
      // console.log(this.data)
      if (this.Form.state === '') {
        // this.data.remove('state')
        delete this.data.state
      } else {
        this.data.state = this.Form.state
      }
      this.data.directoryName = this.Form.directoryName

      this.dataList()
    },
    /**
     * 点击清除
     */
    clear () {
      this.Form.directoryName = ''
      this.Form.state = ''
    },
    /**
     * 返回学科
     */
    toList () {
      this.$router.go(-1)
      // console.log(1)
    }
  }
}
</script>

<style scoped>
.demo-form-inline{
  margin-top: 20px;
}
.back{
  color: #409eff;
  display:inline-block;
  line-height: 36px;

}
.right{
float: right;
margin-bottom: 30px;
}

</style>
